const app = getApp()
var utilMd5 = require('../nextindex/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showRefresh: false, // 正在上拉刷新
    showLoad: false, // 正在下拉加载
    pageData: [],
    pageIndex:1,
    companyEmergencyName:"",
    newvalue:"",
    firstshow:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.setData({
      companyEmergencyName:options.id,
      newvalue:options.id
    })
    wx.getStorage({
      key: 'user',
      success(res) {
        var params = {
          pageIndex: that.data.pageIndex,
          pageSize:20,
          companyId: JSON.parse(res.data).user.companyId,
          companyEmergencyName: options.id
        }
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/plan/list", //仅为示例，并非真实的接口地址
          data: params,
          method: "post",
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          success(res) {
            that.setData({
              firstshow: false
            })
            if (res.data.meta.success){
              if (res.data.data.records){
                that.setData({
                  pageData: res.data.data.records 
                })
                if (res.data.data.records.length>=1){
                  var selectindex = that.data.pageIndex+1;
                  that.setData({
                    pageIndex: selectindex
                  })
                }
              }
            }
          }
        }) 
      }
    })    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    console.log(1)
    setTimeout(() => {
      this.setData({
        ["pageData.list"]: [1, 2, 3]
      })
      wx.stopPullDownRefresh() // 加载完成调用
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '应急预案智能管理小助手',
      path: '/page/nextindex/index'
    }
  },
  scrollTopUpDate: function(e) {
    if (this.data.showLoad) { // 加载完了才能进行下次加载
      return
    }
  },
  scrollBottomUpDate: function(e) {
    if (this.data.showLoad) { // 加载完了才能进行下次加载
      return
    }
    var that = this;
    wx.showLoading({
        mask:true
    })
    this.setData({
      showLoad: true
    })
    if (that.data.pageIndex==1){
      this.setData({
        pageIndex: 2
      })
    }
    wx.getStorage({
      key: 'user',
      success(res) {
        var params = {
          pageIndex: that.data.pageIndex,
          pageSize: 20,
          companyId: JSON.parse(res.data).user.companyId,
          companyEmergencyName: that.data.companyEmergencyName
        }
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/plan/list", //仅为示例，并非真实的接口地址
          data: params,
          method: "post",
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          success(res) {
            if (res.data.meta.success) {
              if (res.data.data.records) {
              
                var arr = [...that.data.pageData, ...res.data.data.records]
                that.setData({
                  pageData: arr
                })
                if (res.data.data.records.length >= 1) {
                  var selectindex = that.data.pageIndex + 1;
                  that.setData({
                    pageIndex: selectindex
                  })
                  that.setData({
                    showLoad: false
                  })
                }
              }
            }else{
              that.setData({
                showLoad: false
              })
            }  
          }
        })
      }
    })  
    wx.hideLoading()
  },
  handlerTabTap(e){
    wx.setStorage({
      key: "urlnewlist",
      data: e.currentTarget.dataset.item.newTemplateFile
    })

    let baseUrl = e.currentTarget.dataset.item.newTemplateFile;
    var urlpath = baseUrl.indexOf("?") + 1;
    urlpath = baseUrl.substring(urlpath);
    var arrapi = urlpath.split("&");
    let uesrMessage = {}
    arrapi.forEach(
      (item) => {
        var brrapi = [];
        brrapi = item.split("=");
        uesrMessage[brrapi[0]] = brrapi[1];
      }
    )
    let id = uesrMessage.srcFid;
    let expires = Date.parse(new Date());
    let a = "FastDFS1234Bossien" + id + expires
    let token = utilMd5.hexMD5(a)
    token = token.toUpperCase()
    wx.getStorage({
      key: 'user',
      success(res) {
        wx.request({
          url: app.globalData.Urlnew +"/queryPublishFileInfo.do?appId=001&srcFid=" + id + "&expires=" + expires + "&token=" + token, //仅为示例，并非真实的接口地址
          data: {},
          method: "GET",
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          success(res) {
            let status = res.data;
            if (!status.success) {
              wx.showToast({
                title: '文件正在转码中，请稍后再查看',
                icon: 'none',
                duration: 1000
              })
              return false;
            }
            if (status.dataObj.srcFileStatusName == "12") {
              wx.navigateTo({
                url: '../detailtest/index'
              })
            } else {
              wx.showToast({
                title: '文件正在转码中，请稍后再查看',
                icon: 'none',
                duration: 1000
              })
            }
          }
        })
      }
    })   
   
  },
  searchname(event) {
    // console.log(event.detail)
    this.setData({
      pageData: [],
      pageIndex: 1,
      companyEmergencyName: this.data.newvalue
    })
    if (this.data.showLoad) { // 加载完了才能进行下次加载
      return
    }
    var that = this;
    wx.showLoading({
      mask: true
    })
    this.setData({
      showLoad: true
    })
    wx.getStorage({
      key: 'user',
      success(res) {
        var params = {
          pageIndex: that.data.pageIndex,
          pageSize: 20,
          companyId: JSON.parse(res.data).user.companyId,
          companyEmergencyName: that.data.companyEmergencyName
        }
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/plan/list", //仅为示例，并非真实的接口地址
          data: params,
          method: "post",
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          success(res) {
            if (res.data.meta.success) {
              if (res.data.data.records) {

                var arr = [...that.data.pageData, ...res.data.data.records]
                that.setData({
                  pageData: arr
                })
                if (res.data.data.records.length >= 20) {
                  var selectindex = that.data.pageIndex + 1;
                  that.setData({
                    pageIndex: selectindex
                  })
                  that.setData({
                    showLoad: false
                  })
                }
              }
            } else {
              that.setData({
                pageIndex: 1
              })
              that.setData({
                showLoad: false
              })
            }
          }
        })
      }
    })
    wx.hideLoading()
  },
  baindinputtaplist(e) {
    this.setData({
      newvalue: e.detail.value
    })
  },
  
})